//
//  XCPushHandler.m
//  EcCommon
//
//  Created by wangguodong on 14-5-19.
//  Copyright (c) 2014年 wuxian. All rights reserved.
//

#import "XCPushHandler.h"
#import "APService.h"
#import "H5ViewController.h"

#define DeviceTokenRegisteredKEY @"DeviceTokenRegisteredKEY"
#define DeviceTokenStringKEY @"DeviceTokenStringKEY"
#define kTag_PushNoti 1400      //消息推送

@interface XCPushHandler () {
    NSDictionary *_pushInfo;        //消息推送信息
}

@end

@implementation XCPushHandler
static XCPushHandler* sharedObj = nil;

+ (id)sharedPushHandler {
    @synchronized (self)
    {
        if (sharedObj == nil)
        {
            sharedObj=[[self alloc] init];
        }
    }
    return sharedObj;
}

- (id)init {
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)configPushWithOptions:(NSDictionary *)launchOptions {
    //注册devicetoken
    
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
            //categories
            [APService
             registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                 UIUserNotificationTypeSound |
                                                 UIUserNotificationTypeAlert)
             categories:nil];
        } else {
            //categories nil
            [APService
             registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                 UIRemoteNotificationTypeSound |
                                                 UIRemoteNotificationTypeAlert)
#else
             //categories nil
             categories:nil];
            [APService
             registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                 UIRemoteNotificationTypeSound |
                                                 UIRemoteNotificationTypeAlert)
#endif
             // Required
             categories:nil];
        }
        [APService setupWithOption:launchOptions];
        
//        [APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
//                                                       UIRemoteNotificationTypeSound |
//                                                       UIRemoteNotificationTypeAlert)
//                                           categories:nil];
//        
//        [APService setupWithOption:launchOptions];
    
    //处理消息推送
    NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
    if (remoteNotification) {
        [self receivePushWithData:remoteNotification];
    }
}

#pragma mark -
#pragma mark 注册令牌相关方法
- (void)didRegisterDeviceToken:(NSData *)deviceToken {
    //将device token转换为字符串
    NSString *deviceTokenStr = [NSString stringWithFormat:@"%@",deviceToken];
    
    //modify the token, remove the  "<, >"
    deviceTokenStr = [[deviceTokenStr substringWithRange:NSMakeRange(0, 72)] substringWithRange:NSMakeRange(1, 71)];
    
    //将deviceToken保存在NSUserDefaults
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    //保存 device token 令牌,并且去掉空格
    [userDefaults setObject:[deviceTokenStr stringByReplacingOccurrencesOfString:@" " withString:@""] forKey:DeviceTokenStringKEY];
    [userDefaults setBool:YES forKey:DeviceTokenRegisteredKEY];
    //将注册到的devicetoken发给jpush
    [APService registerDeviceToken:deviceToken];
    
}

#pragma mark -
#pragma mark 接收到通知的相关方法
- (void)receivePushWithData:(NSDictionary *)userInfo {
    _pushInfo = nil;
    _pushInfo = [userInfo retain];
    
    [APService handleRemoteNotification:userInfo];
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提醒" message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"] delegate:self cancelButtonTitle:@"忽略" otherButtonTitles:@"查看", nil];
//    [alert showWithCompletionHandler:^(NSInteger buttonIndex) {
//        if (buttonIndex==1) {
            if (self.pushDelegate && [self.pushDelegate respondsToSelector:@selector(pushToTargetWithInfo:)]) {
                [self.pushDelegate pushToTargetWithInfo:_pushInfo];
            }
//        }
//    }];
//    [alert release];
}

- (void)dealloc {
    _pushInfo = nil;
    
    [super dealloc];
}
@end
